package com.jp.proxy.httpprocessor;

import com.google.gson.Gson;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @ProjectName : HttpProcessor_20200413
 * @Author : Jason
 * @Time : 2021/8/29 16:38
 * @Description : 文件描述
 */
public abstract class HttpCallBack<RESULT> implements IRequestCallBack {

    private static final String TAG = HttpCallBack.class.getSimpleName();

    @Override
    public void onSuccess(String responseStr) {
        Class<?> tClass = analysisClassInfo(this);
        RESULT result = (RESULT) new Gson().fromJson(responseStr, tClass);
        onSuccess(result);
    }

    public abstract void onSuccess(RESULT result);

    public abstract void onFailure(String msg);
    //获取此类上的泛型的class对象
    public Class<?> analysisClassInfo(Object object) {
        Class<?> aClass = object.getClass();
        Type genericSuperclass = aClass.getGenericSuperclass();
        Type[] actualTypeArguments = ((ParameterizedType) genericSuperclass).getActualTypeArguments();
        return (Class<?>) actualTypeArguments[0];
    }
}
